/*  

Cafechess Library: Cover basic chess programming needs, like GUI, Engine integration, 
FEN and PGN parsing.

                 \\|//
                 (o o)
--------------ooO-(_)-Ooo----------------------------------
Copyright (C) 2009  By Gregory A. Prentice Snoqualmie, WA
                    www.cafechess.org
                    cafechess@comcast.net
                      Oooo
--------------oooO----(  )---------------------------------
              (  )    (_/
               \_)

Microsoft Public License (Ms-PL)
               
Revisions:

Author        Date    Description:

*/
using System;

namespace Cafechess.General
{
	/// <summary>
	/// Implements the Disposable Pattern allowing the base class
	/// to simply focus on the methods DisposedManaged() and 
	/// DisposedUnmanaged().  In both of these methods you should
	/// always check the IsDisposed
	/// </summary>
	public abstract class Disposable :IDisposable
	{
    #region Members
    private bool coIsDisposed;
    #endregion Members
    
    #region Properties
    /// <summary>
    /// True if this resource has been disposed of.
    /// </summary>
    public bool IsDisposed
    {
      get{ return coIsDisposed; }
    }
    #endregion Properties

    #region Constructors
    /// <summary>
    /// Ensure the disposed flag is flase.
    /// </summary>
    public Disposable()
		{
      coIsDisposed = false;
    }
    /// <summary>
    /// Finalizer code to make sure we call our Dispose method.
    /// </summary>
    ~Disposable()
    {
      // Calling with false lets us know we are in the finalizer.
      Dispose(false);
    }
    #endregion Constructors    
    #region Abstract
    /// <summary>
    /// Implement any Managed cleanup that you need to do.
    /// </summary>
    protected abstract void DisposeManaged();
    /// <summary>
    /// Most important is the clean up of any Unmanaged resources such as
    /// window or file handles.
    /// </summary>
    protected abstract void DisposeUnmanaged();
    #endregion Abstract

    #region IDisposable Members
    /// <summary>
    /// Our public method for cleaning up this object.
    /// </summary>
    public void Dispose()
    {
      Dispose( true );
      GC.SuppressFinalize( this );
    }
    protected void Dispose( bool isFinalizing )
    {
      if(!coIsDisposed)
      {
        // If isFinalizing equals true, dispose all managed resources.
        if(isFinalizing)
        {
          DisposeManaged();
        }            
        // Call the appropriate methods to clean up 
        // unmanaged resources here.
        DisposeUnmanaged();
        // Show that we've cleaned up our mess.
        coIsDisposed = true;
      }
    }
    #endregion IDisposable Members
  }
}
